#ifndef WINDY_ADDTILESET_HPP
#define WINDY_ADDTILESET_HPP

#include <wx/wxprec.h>

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include <wx/sharedptr.h> 

#include "constants.hpp"
//#include "monkey_prefs.hpp"

class WindyAddTileset : public wxDialog
{
public:
   WindyAddTileset (wxWindow *parent, const wxString &title, wxImageList &icons/*, MonkeyPrefs &pref*/, const wxSize &size = wxDefaultSize) :
   wxDialog(parent, wxID_ANY, title, wxDefaultPosition, size), tiles(new wxImageList())/*, prefs(pref)*/
   {
      wxBoxSizer *fname_sz = new wxBoxSizer(wxHORIZONTAL);
      wxTextCtrl *fnametxt = new wxTextCtrl(this, WindyAddTileset_File);
      wxBitmapButton *fnamebtn = new wxBitmapButton(this, WindyAddTileset_Browse, icons.GetBitmap(WindyBmp_Browse));
      fname_sz->Add(new wxStaticText(this, wxID_ANY, _("File")), wxSizerFlags().Border(wxRIGHT | wxTOP, 5));
      fname_sz->Add(fnametxt, wxSizerFlags(1).Expand().Border(wxRIGHT, 5));
      fname_sz->Add(fnamebtn, wxSizerFlags().Right());

      wxStaticBoxSizer *tsinfo_sz = new wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, _("Tileset info")), wxVERTICAL);
      wxFlexGridSizer *tsinfogrid_sz = new wxFlexGridSizer(2, 2, 5, 5);
      tsinfogrid_sz->Add(new wxStaticText(this, wxID_ANY, _("Name")), wxSizerFlags().Right());
      tsinfogrid_sz->Add(new wxTextCtrl(this, WindyAddTileset_Name), wxSizerFlags().Expand());
      tsinfogrid_sz->Add(new wxStaticText(this, wxID_ANY, _("Description")), wxSizerFlags().Right());
      tsinfogrid_sz->Add(new wxTextCtrl(this, WindyAddTileset_Desc), wxSizerFlags().Expand());
      tsinfogrid_sz->SetFlexibleDirection(wxHORIZONTAL);
      tsinfogrid_sz->AddGrowableCol(1);
      tsinfo_sz->Add(tsinfogrid_sz, wxSizerFlags().Expand().Border(wxALL, 5));

      wxBoxSizer *buttons_sz = new wxBoxSizer(wxHORIZONTAL);
      wxButton *btnok = new wxButton(this, wxID_OK, _("Add"));
      wxButton *btncancel = new wxButton(this, wxID_CANCEL, _("Cancel"));
      buttons_sz->AddStretchSpacer(1);
      buttons_sz->Add(btnok, wxSizerFlags().Right().Border(wxRIGHT, 5));
      buttons_sz->Add(btncancel, wxSizerFlags().Right());

      wxBoxSizer *global_sz = new wxBoxSizer(wxVERTICAL);
      global_sz->Add(fname_sz, wxSizerFlags().Expand().Border(wxLEFT | wxRIGHT | wxTOP, 10));
      global_sz->AddSpacer(10);
      global_sz->Add(tsinfo_sz, wxSizerFlags().Expand().Border(wxLEFT | wxRIGHT, 10));
      global_sz->AddStretchSpacer();
      global_sz->Add(buttons_sz, wxSizerFlags().Expand().Border(wxLEFT | wxRIGHT | wxBOTTOM, 10));

      SetSizer(global_sz);
   }

   ~WindyAddTileset () {
   }

   void OnOk (wxCommandEvent &event)
   {
      /*
      prefs.setBool(wxT("window-centered"), dynamic_cast <wxCheckBox *> (FindWindowById(MonkeyOptions_AlwaysCenter))->GetValue());
      prefs.setBool(wxT("remember-size"), dynamic_cast <wxCheckBox *> (FindWindowById(MonkeyOptions_RememberSize))->GetValue());
      prefs.setBool(wxT("remember-position"), dynamic_cast <wxCheckBox *> (FindWindowById(MonkeyOptions_RememberPos))->GetValue());
      prefs.setBool(wxT("remember-ui-state"), dynamic_cast <wxCheckBox *> (FindWindowById(MonkeyOptions_RememberUI))->GetValue());

      wxRadioButton *hex_offset = dynamic_cast <wxRadioButton *> (FindWindowById(MonkeyOptions_OffsetHex));
      prefs.set(wxT("results-offset-mode"), hex_offset->GetValue() ? wxT("hex") : wxT("dec"));

      wxSpinCtrl *sb_size = dynamic_cast <wxSpinCtrl *> (FindWindowById(MonkeyOptions_SearchBuffer));
      prefs.setInt(wxT("search-buffer-size"), sb_size->GetValue() * 1048576);

      wxSpinCtrl *prvw_width = dynamic_cast <wxSpinCtrl *> (FindWindowById(MonkeyOptions_PreviewWidth));
      prefs.setInt(wxT("preview-width"), prvw_width->GetValue());*/

      
      EndModal(event.GetId());
   }

   void OnBrowse (wxCommandEvent &event)
   {
      
      const wxString filter = _("PNG Images (*.png)|*.png");
      const int style = wxFD_OPEN | wxFD_FILE_MUST_EXIST;

      wxFileDialog d(this, _("Pick a tileset image."), wxEmptyString, wxEmptyString, filter, style );

      if (d.ShowModal() == wxID_OK)
      {
         dynamic_cast <wxTextCtrl *> (FindWindowById(WindyAddTileset_File))->SetValue(d.GetPath());

         wxImage ts(d.GetPath(), wxBITMAP_TYPE_PNG);
         tiles->Create(32, 16, true);

         wxSize ntiles(ts.GetWidth() / 32, ts.GetHeight() / 16);

         // split the tiles
         for (int i = 0; i < ntiles.y * ntiles.x; i++)
         {
            wxRect r((i % ntiles.x) * 32, int(i / ntiles.x) * 16, 32, 16);
            wxImage t = ts.GetSubImage(r);
            
            bool empty = true;

            // get only the filled tiles (not transparent)
            for (int y = 0; y < 16; y++)
               for (int x = 0; x < 32; x++)
                  if (!t.IsTransparent(x, y, wxIMAGE_ALPHA_TRANSPARENT))
                     empty = false;

            if (!empty)
               tiles->Add(t);
         }

         img.Create(ts.GetWidth() + ntiles.x + 1, ts.GetHeight() + ntiles.y + 1);
         {
            wxMemoryDC mdc(img);

            mdc.SetBrush(*wxWHITE_BRUSH);
            mdc.Clear();

            mdc.SetBrush(*wxTheBrushList->FindOrCreateBrush(wxColor(0xcccccc)));
            mdc.SetPen(*wxTRANSPARENT_PEN);

            for (int y = 0; y <= img.GetHeight() / 8; y++)
               for (int x = (y % 2 ? 0 : 1); x <=  img.GetWidth() / 8; x += 2)
                  mdc.DrawRectangle(x * 8, y * 8, 8, 8);

            mdc.SetPen(*wxBLACK_PEN);

            // vertical lines
            for (int x = 0; x <= ntiles.x; x++)
               mdc.DrawLine(x * 32 + x, 0, x * 32 + x, img.GetHeight());

            // horizontal lines
            for (int y = 0; y <= ntiles.y; y++)
               mdc.DrawLine(0, y * 16 + y, img.GetWidth(), y * 16 + y);

            for (int i = 0; i < ntiles.x * ntiles.y; i++)
            {
               wxPoint pos(
                  1 + (i % ntiles.x) * 32 + (i % ntiles.x),
                  1 + int(i / ntiles.x) * 16 + int(i / ntiles.x)
               );

               if (i < tiles->GetImageCount())
                  mdc.DrawBitmap(tiles->GetBitmap(i), pos.x, pos.y, true);
               else
               {
                  mdc.DrawLine(pos.x, pos.y, pos.x + 32, pos.y + 16);
                  mdc.DrawLine(pos.x + 32, pos.y, pos.x, pos.y + 16);
               }
            }
         }

         Refresh(), Update();
      }
   }

   void OnPaint (wxPaintEvent &event)
   {
      wxPaintDC dc(this);
      
      if (img.IsOk())
      {
         wxStaticBoxSizer *szr = dynamic_cast <wxStaticBoxSizer *>(GetSizer()->GetItem(2)->GetSizer());
         wxRect r = szr->GetStaticBox()->GetRect();

         dc.DrawBitmap(wxBitmap(img), r.x, r.y + r.height + 10);
      }
   }

   wxString getFilename () const
   { return dynamic_cast <wxTextCtrl *>(FindWindowById(WindyAddTileset_File))->GetValue(); }

   wxString getName () const
   { return dynamic_cast <wxTextCtrl *>(FindWindowById(WindyAddTileset_Name))->GetValue(); }

   wxString getDescription () const
   { return dynamic_cast <wxTextCtrl *>(FindWindowById(WindyAddTileset_Desc))->GetValue(); }

   wxSharedPtr <wxImageList> getTileset () const { return tiles; }

private:
   //MonkeyPrefs &prefs;
   wxSharedPtr <wxImageList> tiles;
   wxBitmap img;

   DECLARE_EVENT_TABLE()
};

BEGIN_EVENT_TABLE(WindyAddTileset, wxDialog)
   EVT_BUTTON(WindyAddTileset_Browse, WindyAddTileset::OnBrowse)
   EVT_BUTTON(wxID_OK, WindyAddTileset::OnOk)
   EVT_PAINT(WindyAddTileset::OnPaint)
END_EVENT_TABLE()

#endif //~WINDY_NEWDOC_HPP